package com.kamistoat.meimeistore.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.meimeistore.ware.vo.MergeVo;
import com.kamistoat.meimeistore.ware.vo.PurchaseFinishVo;
import com.kamistoat.meimeistore.ware.entity.PurchaseEntity;
import com.kamistoat.meimeistore.ware.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;



/**
 * 采购信息
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 16:45:27
 */
@RestController
@RequestMapping("meimeistoreware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 获取新建/已分配的采购单
     * @param params
     * @return
     */
    @RequestMapping("unreceive/list")
    //@RequiresPermissions("meimeistoreware:purchase:list")
    public R unreceiveList(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPageUnreceive(params);

        return R.ok().put("page", page);
    }

    /**
     * 将采购需求合并成采购单
     * @param mergeVo
     * @return
     */
    @RequestMapping("/merge")
    public R merge(@RequestBody MergeVo mergeVo){
        purchaseService.mergePurchase(mergeVo);
        return R.ok();
    }

    /**
     * 获取分配给自己的采购单
     * @param assignee_name 采购人员名字
     * @return
     */
    @RequestMapping("/getASSIGNED/{assignee_name}")
    public R getASSIGNED(@PathVariable("assignee_name") String assignee_name){
        List<Long> ASSIGNEDids = purchaseService.getASSIGNED(assignee_name);
        return R.ok().put("data",ASSIGNEDids);
    }

    /**
     * 提交采购员选择的采购单ID
     * @param purchaseIds
     * @return
     */
    @RequestMapping("/postRECEIVED")
    public R postRECEIVED(@RequestBody List<Long> purchaseIds){
        purchaseService.postRECEIVED(purchaseIds);
        return R.ok();
    }

    /**
     * 采购员完成采购单
     */
    @RequestMapping("/done")
    public R postFINISHED(@RequestBody PurchaseFinishVo purchaseFinishVo){
        purchaseService.postFINISHED(purchaseFinishVo);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meimeistoreware:purchase:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("meimeistoreware:purchase:info")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meimeistoreware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meimeistoreware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meimeistoreware:purchase:delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
